package com.hb0730.boot.admin.modules.sci.controller;

import com.hb0730.boot.admin.base.R;
import com.hb0730.boot.admin.data.domain.BasePage;
import com.hb0730.boot.admin.modules.sci.model.bo.SciExportBo;
import com.hb0730.boot.admin.modules.sci.model.entity.SciPaperConference;
import com.hb0730.boot.admin.modules.sci.model.query.SciPaperConferenceQuery;
import com.hb0730.boot.admin.modules.sci.service.SciPaperConferenceService;
import jakarta.servlet.http.HttpServletResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Optional;

@RestController
@RequestMapping("/sci/paper/conference")
@Tag(name = "科研：论文管理：会议论文")
@RequiredArgsConstructor
public class SciPaperConferenceController {

    private final SciPaperConferenceService sciPaperConferenceService;

    @GetMapping("/query/page")
    @Operation(summary = "分页查询")
    @PreAuthorize("hasAuthority('sci:paper:conference:query')")
    public R<BasePage<SciPaperConference>> queryPage(HttpServletRequest request, @ParameterObject SciPaperConferenceQuery query) {
        BasePage<SciPaperConference> res = this.sciPaperConferenceService.queryPage(query);
        return R.OK(res);
    }

    @PostMapping("/save")
    @Operation(summary = "保存")
    public R<SciPaperConference> save(HttpServletRequest request, @Validated @RequestBody SciPaperConference sciPaperConference) {
        return this.sciPaperConferenceService.saveConference(sciPaperConference);
    }

    @PutMapping("/update/{id}")
    @Operation(summary = "修改")
    public R<SciPaperConference> update(HttpServletRequest request, @PathVariable String id,
                            @Validated @RequestBody SciPaperConference sciPaperConference) {
        return this.sciPaperConferenceService.updateById(id, sciPaperConference);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除")
    public R<String> delete(HttpServletRequest request, @Validated @RequestBody List<String> ids) {
        if (null == ids || ids.isEmpty()) {
            return R.NG("删除失败，缺少参数");
        }
        return this.sciPaperConferenceService.deleteByIds(ids);
    }
    @PostMapping(value = "/exportData")
    public void exportData(HttpServletResponse response , @Validated @RequestBody SciExportBo sciExportBo) {
        if(sciExportBo.getQuery()!=null) this.sciPaperConferenceService.exportData(response, sciExportBo);
    }
}